<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>类的prototype属性和__proto__属性</title>
</head>
<body>
    <h1>类的prototype属性和__proto__属性</h1>

    <script>
        // 子类的__proto__属性指向父类
        class Parent {}
        class Child extends Parent {}

        console.log(Child.__proto__ === Parent); // true
        console.log(Child.prototype.__proto__ === Parent.prototype); // true

        // 实例的__proto__属性
        const child = new Child();
        console.log(child.__proto__ === Child.prototype); // true

        // 多层继承
        class Animal {
            constructor(name) {
                this.name = name;
            }
        }

        class Mammal extends Animal {
            constructor(name) {
                super(name);
                this.warmBlooded = true;
            }
        }

        class Dog extends Mammal {
            constructor(name, breed) {
                super(name);
                this.breed = breed;
            }
        }

        // 原型链关系
        console.log(Dog.__proto__ === Mammal); // true
        console.log(Mammal.__proto__ === Animal); // true
        console.log(Dog.prototype.__proto__ === Mammal.prototype); // true
        console.log(Mammal.prototype.__proto__ === Animal.prototype); // true

        // 实际应用：原型链检查
        function isInheritedFrom(childClass, parentClass) {
            return childClass.prototype instanceof parentClass;
        }

        console.log(isInheritedFrom(Dog, Animal)); // true
        console.log(isInheritedFrom(Dog, Mammal)); // true
        console.log(isInheritedFrom(Mammal, Animal)); // true
    </script>
</body>
</html> 